package com.jiamao.goplay.ui.user;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import me.goldze.mvvmhabit.utils.KLog;

import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.adapter.UserWanToGoAdapter;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.databinding.MeHomeLayoutBinding;
import com.jiamao.goplay.ui.user.vm.MeVM;
import com.jiamao.goplay.view.HorizontalItemDecoration;

public class MeFragment extends BaseMVVMFragment<MeHomeLayoutBinding, MeVM> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.me_home_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public MeVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MeVM.class);
    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.pSwitchEvent.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                binding.refresh.setRefreshing(false);
            }
        });
        viewModel.uc.pRenxiangEvent.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer aBoolean) {
                ((TextView)binding.getRoot().findViewById(R.id.tv_renxiang)).setText(aBoolean==1?"已认证":"未认证");
            }
        });
        viewModel.uc.pShimingEvent.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer aBoolean) {
                ((TextView)binding.getRoot().findViewById(R.id.tv_shiming)).setText(aBoolean==1?"已认证":"未认证");
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        ImmersionBar.with(this). keyboardEnable(false).statusBarDarkFont(true).titleBar(binding.getRoot().findViewById(R.id.toolbar)).init();

        binding.setAdapter(new UserWanToGoAdapter(getContext()));

        RecyclerView recyclerViewGotoList = binding.getRoot().findViewById(R.id.user_goto_listview);
        RecyclerView recyclerPhotoList = binding.getRoot().findViewById(R.id.user_photo_listview);

        recyclerViewGotoList.addItemDecoration(new HorizontalItemDecoration(11,getContext()));//10表示10dp
        recyclerPhotoList.addItemDecoration(new HorizontalItemDecoration(9,getContext()));//10表示10dp
        binding.refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                viewModel.getUserInfoDetail();
               // binding.refresh.setRefreshing(false);
            }
        });
        recyclerPhotoList.setItemAnimator(null);
        recyclerViewGotoList.setItemAnimator(null);
//        binding.refresh.e÷
       // binding.refresh.setRefreshing(true);
        viewModel.getUserInfoDB();
       // viewModel.getUserInfoDetail();

    }

    @Override
    public void onResume() {
        super.onResume();
        //每次从数据库读取资料
        KLog.d("html","onReseume ");
        viewModel.getInitUserLoad();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden){
            viewModel.getInitUserLoad();
        }
    }
}
